﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CompareWorkerThread.cs" company="SmartPlan">
//   SmartPlan 2011
// </copyright>
// <summary>
//   Defines the CompareWorkerThread type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------


using DbDiff.Common.Model;

namespace DbDiff.WinClient.Helper
{
    using System;
    using System.Threading;
    using Common.Comparer;
    using Common.Helper;

    public class CompareWorkerThread : WorkerThreadBase 
    {
        #region Events & delegates
        public delegate void OnCompareThreadEventHandler(object sender, CompareEventArgs e);
        public delegate void OnCompareInfoThreadEventHandler(object sender, CompareInfoEventArgs e);

        public event OnCompareThreadEventHandler OnCompare;
        public event OnCompareInfoThreadEventHandler OnCompareStart;
        public event OnCompareInfoThreadEventHandler OnCompareEnd;
        #endregion Events & delegates

        #region Fields & properties
        private DatabaseObjects _dbInfo1;
        private DatabaseObjects _dbInfo2;
        private DatabaseCompare databaseCompare;

        public DatabaseCompare DatabaseCompare
        {
            get {return this.databaseCompare;}
        }
        #endregion Fields & properties

        #region Constructors
        public CompareWorkerThread(DatabaseObjects dbInfo1, DatabaseObjects dbInfo2)
            : base()
        {
            _dbInfo1 = dbInfo1;
            _dbInfo2 = dbInfo2;
            this.databaseCompare = new DatabaseCompare(dbInfo1, dbInfo2);

            this.databaseCompare.OnCompare += new DatabaseCompare.CompareEventHandler(CompareHandler);
            this.databaseCompare.OnStart += new DatabaseCompare.CompareStartEventHandler(CompareStartHandler);
            this.databaseCompare.OnEnd += new DatabaseCompare.CompareEndEventHandler(CompareEndHandler);
        }
        #endregion Constructors

        #region IWorkerThread Members
        public override void Start() 
        {
            //várjon egy kicsit, a form megjelenésére...
            Thread.Sleep(100);
            this.databaseCompare.Compare(ConfigInstance.Config.CompareOptions);
        }
        #endregion
        
        private void CompareStartHandler(object sender, EventArgs e) 
        {
            if (OnCompareStart != null)
                OnCompareStart(this, new CompareInfoEventArgs("Starting..."));
        }
        private void CompareEndHandler(object sender, CompareResultEventArgs e) 
        {
            if (OnCompareEnd != null)
                OnCompareEnd(this, new CompareInfoEventArgs("Compare finished!"));
        }
        private void CompareHandler(object sender, CompareEventArgs e) 
        {
            if (OnCompare != null)
                OnCompare(this, new CompareEventArgs(e.ObjectType, e.ObjectName));
        }
    }
}